package com.almeros.android.multitouch.library;

import ohos.multimodalinput.event.TouchEvent;

public class ScaleGestureDetector {

    private static final int INVALID_POINTER_INDEX = -1;
    private float fX, fY, sX, sY;
    private int mPointerIndex1, mPointerIndex2;
    private float mScale = 1;

    private OnScaleGestureListener mListener;
    private float tempV;

    public ScaleGestureDetector(OnScaleGestureListener listener) {
        mListener = listener;
        mPointerIndex1 = INVALID_POINTER_INDEX;
        mPointerIndex2 = INVALID_POINTER_INDEX;
    }

    public float getScale() {
        return mScale;
    }

    public boolean onTouchEvent(TouchEvent event) {
        switch (event.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                sX = event.getPointerPosition(0).getX();
                sY = event.getPointerPosition(0).getY();
                mPointerIndex1 = event.getPointerId(0);
                break;
            case TouchEvent.OTHER_POINT_DOWN:
                fX = event.getPointerPosition(0).getX();
                fY = event.getPointerPosition(0).getY();
                mPointerIndex2 = event.getPointerId(event.getIndex());
                break;
            case TouchEvent.POINT_MOVE:
                if (mPointerIndex1 != INVALID_POINTER_INDEX && mPointerIndex2 != INVALID_POINTER_INDEX
                        && event.getPointerCount() > mPointerIndex2) {

                    float nfX, nfY, nsX, nsY;

                    nsX = event.getPointerPosition(mPointerIndex1).getX();
                    nsY = event.getPointerPosition(mPointerIndex1).getY();
                    nfX = event.getPointerPosition(mPointerIndex2).getX();
                    nfY = event.getPointerPosition(mPointerIndex2).getY();
                    calculateScaleBetweenLines(nfX, nfY, nsX, nsY);
                    if (mListener != null) {
                        mListener.onScale(this);
                    }
                    tempV = Math.abs(fX - sX) * Math.abs(fX - sX) + Math.abs(fY - sY) * Math.abs(fY - sY);
                    fX = nfX;
                    fY = nfY;
                    sX = nsX;
                    sY = nsY;
                }
                break;
            case TouchEvent.PRIMARY_POINT_UP:
                mPointerIndex1 = INVALID_POINTER_INDEX;
                break;
            case TouchEvent.OTHER_POINT_UP:
                mPointerIndex2 = INVALID_POINTER_INDEX;
                break;
        }
        return true;
    }

    private void calculateScaleBetweenLines(float sx1, float sy1, float sx2, float sy2) {
        float absX = Math.abs(fX - sX);
        float absY = Math.abs(fY - sY);

        float absXt = Math.abs(sx1 - sx2);
        float absYt = Math.abs(sy1 - sy2);

        if (Math.abs(sqrt(Math.abs(fX - sX) * Math.abs(fX - sX)
                + Math.abs(fY - sY) * Math.abs(fY - sY)) - sqrt(tempV)) > 3) {

            if (absX > absXt || absY > absYt) {
                if (mScale > 0.8f) {
                    mScale = mScale - 0.05f;
                }
            }
            if (absX < absXt || absY < absYt) {
                mScale = mScale + 0.05f;
                if (mScale > 12) {
                    mScale = 12;
                }
            }
        }
    }

    private float sqrt(float m) {
        float i = 0;
        while ((i * i) <= m) {
            i += 0.1;
        }

        for (int j = 0; j < 10; j++) {
            if (i > 0){
                i = (m / i + i) / 2;
            }
        }
        return i;
    }

    public static class SimpleOnScaleGestureListener implements OnScaleGestureListener {

        @Override
        public boolean onScale(ScaleGestureDetector scaleDetector) {
            return false;
        }
    }

    public interface OnScaleGestureListener {

        boolean onScale(ScaleGestureDetector scaleDetector);
    }
}
